﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.Party.PartyComponents.VillagerPartyComponent
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System.Collections.Generic;
using TaleWorlds.CampaignSystem.CharacterDevelopment;
using TaleWorlds.CampaignSystem.Extensions;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.Party.PartyComponents
{
  public class VillagerPartyComponent : PartyComponent
  {
    [CachedData]
    private TextObject _cachedName;

    internal static void AutoGeneratedStaticCollectObjectsVillagerPartyComponent(
      object o,
      List<object> collectedObjects)
    {
      ((PartyComponent) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this.Village);
    }

    internal static object AutoGeneratedGetMemberValueVillage(object o)
    {
      return (object) ((VillagerPartyComponent) o).Village;
    }

    public static MobileParty CreateVillagerParty(
      string stringId,
      Village village,
      int villagerPartySize)
    {
      return MobileParty.CreateParty(stringId, (PartyComponent) new VillagerPartyComponent(village), (PartyComponent.OnPartyComponentCreatedDelegate) (mobileParty => (mobileParty.PartyComponent as VillagerPartyComponent).InitializeVillagerPartyProperties(villagerPartySize)));
    }

    [SaveableProperty(1)]
    public Village Village { get; private set; }

    public override Hero PartyOwner => this.Village.Settlement.OwnerClan.Leader;

    public override TextObject Name
    {
      get
      {
        if (this._cachedName == null)
        {
          this._cachedName = GameTexts.FindText("str_villagers_of_VILLAGE_NAME");
          this._cachedName.SetTextVariable("VILLAGE_NAME", this.Village.Name);
        }
        return this._cachedName;
      }
    }

    public override Settlement HomeSettlement => this.Village.Settlement;

    protected internal VillagerPartyComponent(Village village) => this.Village = village;

    protected override void OnInitialize() => this.Village.VillagerPartyComponent = this;

    protected override void OnFinalize()
    {
      this.Village.VillagerPartyComponent = (VillagerPartyComponent) null;
    }

    public override void ClearCachedName() => this._cachedName = (TextObject) null;

    private void InitializeVillagerPartyProperties(int villagerPartySize)
    {
      PartyTemplateObject villagerPartyTemplate = this.Village.Settlement.Culture.VillagerPartyTemplate;
      this.Party.MobileParty.Aggressiveness = 0.0f;
      if (this.Village.Bound?.Town?.Governor != null && this.Village.Bound.Town.Governor.GetPerkValue(DefaultPerks.Scouting.VillageNetwork))
        villagerPartySize = MathF.Round((float) villagerPartySize * (1f + DefaultPerks.Scouting.VillageNetwork.SecondaryBonus));
      if ((double) villagerPartySize > (double) this.Village.Hearth)
        villagerPartySize = (int) this.Village.Hearth;
      this.Village.Hearth -= (float) ((villagerPartySize + 1) / 2);
      this.Party.MobileParty.InitializeMobilePartyAroundPosition(villagerPartyTemplate, this.Village.Owner.Settlement.Position2D, 1f, troopNumberLimit: villagerPartySize);
      this.Party.SetVisualAsDirty();
      this.Party.MobileParty.InitializePartyTrade(0);
      float num = 10000f;
      ItemObject itemObject1 = (ItemObject) null;
      foreach (ItemObject itemObject2 in (List<ItemObject>) Items.All)
      {
        if (itemObject2.ItemCategory == DefaultItemCategories.PackAnimal && (double) itemObject2.Value < (double) num && itemObject2.Value > 40)
        {
          itemObject1 = itemObject2;
          num = (float) itemObject2.Value;
        }
      }
      if (itemObject1 == null)
        return;
      int amount = (int) (0.5 * (double) villagerPartySize);
      this.MobileParty.ItemRoster.Add(new ItemRosterElement(itemObject1, amount));
    }
  }
}
